package com.dpkit.annotation.selector;

import com.dpkit.annotation.EnableDPKit;
import com.dpkit.configuration.DPKitAutoConfiguration;
import com.dpkit.enums.DatabaseType;
import com.dpkit.enums.ORMType;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 根据配置选择不同的自动配置类
 */
public class DPKitConfigurationSelector implements ImportSelector {
    
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        Map<String, Object> attributes = importingClassMetadata.getAnnotationAttributes(
            EnableDPKit.class.getName());
        
        ORMType ormType = (ORMType) attributes.get("ormType");
        DatabaseType dbType = (DatabaseType) attributes.get("databaseType");
        
        List<String> configurations = new ArrayList<>();
        configurations.add(DPKitAutoConfiguration.class.getName());
        
        // 根据ORM类型添加特定配置
        if (ormType == ORMType.AUTO || ormType == ORMType.HIBERNATE) {
            configurations.add(DPKitHibernateConfiguration.class.getName());
        }
        if (ormType == ORMType.AUTO || ormType == ORMType.MYBATIS) {
            configurations.add(DPKitMyBatisConfiguration.class.getName());
        }
        
        // 根据数据库类型添加方言配置
        if (dbType != DatabaseType.AUTO) {
            configurations.add("com.dpkit.dialect." + dbType.name() + "DialectConfiguration");
        }
        
        return configurations.toArray(new String[0]);
    }
}